package com.aso114.project.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.OpenVipAdapter;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.PayNumBean;
import com.aso114.project.bean.PriceListBean;
import com.aso114.project.commonview.MyListView;
import com.aso114.project.dialog.PayDialog;
import com.aso114.project.mvp.alipay.H5PayDemoActivity;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SystemBarHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OpenVipActivity extends BaseSimpleActivity implements OpenVipAdapter.OnItemClickListener {
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.layout_title_bar_right_iv)
    ImageView layoutTitleBarRightIv;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    @BindView(R.id.open_vip_price)
    TextView openVipPrice;
    @BindView(R.id.open_vip_listView)
    MyListView openVipListView;
    @BindView(R.id.open_check)
    TextView openCheck;
    @BindView(R.id.open_vip_protocol)
    TextView openVipProtocol;
    @BindView(R.id.open_vip_money)
    TextView openVipMoney;
    @BindView(R.id.open_vip_btn)
    TextView openVipBtn;
    @BindView(R.id.vip_view_title)
    TextView vipViewTitle;
    private List<PriceListBean> priceListBean = new ArrayList<>();// （遍历后的数据）listview遍历后 adapter用到的数据
    private List<PriceListBean> baseBean = new ArrayList<>();// （全部数据）外部用到的数据，方便遍历使用
    private List<PriceListBean> dialogBean = new ArrayList<>();// （遍历后的数据）dialog里面使用的数据
    OpenVipAdapter openVipAdapter;
    private double minPrice = 10000;//默认一个最低价格（要大于每月价格才好替换）
    private boolean isCheck;// checkbox 点击状态
    public static OpenVipActivity instance ;
    @Override
    protected int getResouseId() {
        return R.layout.activity_open_vip;
    }

    @Override
    protected void initView() {

        instance = this;
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        layoutTitleBarTitleTv.setText("开通会员");
        vipViewTitle.setText(getString(R.string.app_name)+"VIP");
        openVipAdapter = new OpenVipAdapter(this);
        openVipListView.setAdapter(openVipAdapter);
        openVipAdapter.setOnItemClickListener(this);
        // 获取数据，并遍历，adapter使用
        CommentModel.getInstant().getPriceList(new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                baseBean = (List<PriceListBean>) o;
                priceListBean.clear();
                if (baseBean == null || baseBean.isEmpty()) {
                    return;
                }
                for (int i = 0; i < baseBean.size(); i++) {
                    if ("1".equals(baseBean.get(i).getIsShow())) {
                        priceListBean.add(baseBean.get(i));
                    }
                }
                for (int p = 0; p < priceListBean.size(); p++) {
                    double price = priceListBean.get(p).getVipPrice() / (priceListBean.get(p).getVipNum() / 30);
                    if (minPrice > price) {
                        minPrice = price;
                    }
                }
                openVipAdapter.setData(priceListBean);
                openVipAdapter.notifyDataSetChanged();
                priceListBean.get(1).setClick(true);
                openVipPrice.setText(String.format("%.2f", minPrice));
                openVipMoney.setText("合计：￥" + String.format("%.2f", priceListBean.get(1).getVipPrice()));
            }

            @Override
            public void onFailure(String fail) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.open_check, R.id.open_vip_protocol, R.id.open_vip_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.open_check:
                if (isCheck) {
                    Drawable drawableLeft = getResources().getDrawable(
                            R.mipmap.buy_xy1_ic);
                    openCheck.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
                    isCheck = false;
                } else {
                    Drawable drawableLeft = getResources().getDrawable(
                            R.mipmap.buy_xy2_ic);
                    openCheck.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, null, null, null);
                    isCheck = true;
                }
                break;
            case R.id.open_vip_protocol:
                break;
            case R.id.open_vip_btn:
                if (!isCheck) {
                    Helper.showToast("请先阅读并勾选《会员服务协议》");
                    return;
                }
                PayDialog dialog = new PayDialog(this, getDialogBean(), new PayDialog.OnDialogClick() {
                    @Override
                    public void onZfb(PriceListBean bean) {

                        payoeder("1",bean.getId());

                    }

                    @Override
                    public void onWx(PriceListBean bean) {

                        if (Helper.isWeixinAvilible(OpenVipActivity.this) == true) {
                            payoeder("2",bean.getId());

                        } else {
                            Helper.showToast("您未安装微信,不能使用微信支付");
                        }
                    }
                });
                dialog.show();


                break;
        }
    }


    public void payoeder(final String paytype,String vipid){

        CommentModel.getInstant().setCreate(paytype,  vipid, new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                Helper.gotoLogin(isSuccess);//token失效去重新登陆


                PayNumBean payNumBean = (PayNumBean) o;
                if (payNumBean==null){
                    return;
                }

                if(paytype.equals("1")){
                    Intent intent = new Intent(OpenVipActivity.this, H5PayDemoActivity.class);
                    intent.putExtra("orderNo", payNumBean.getOrderNo());
                    startActivity(intent);
                }
                if(paytype.equals("2")){

                    Intent intent = new Intent(OpenVipActivity.this, WxPayOrderActivity.class);
                    intent.putExtra("orderNo", payNumBean.getOrderNo());
                    startActivity(intent);
                }
            }

            @Override
            public void onFailure(String fail) {

            }
        });

    }

    private List<PriceListBean> getDialogBean(){
        double vipNum = 0;
        dialogBean.clear();
        for (int i = 0; i < priceListBean.size(); i++) {
            if (priceListBean.get(i).isClick()) {
                vipNum = priceListBean.get(i).getVipNum();
            }
        }
        for (int d = 0; d < baseBean.size(); d++) {
            if (vipNum == baseBean.get(d).getVipNum()) {
                dialogBean.add(baseBean.get(d));
            }
        }
        return dialogBean;
    }

    @Override
    public void onItemClick(int position) {// adapter 的item点击事件（切换点击状态）
        for (int c = 0; c < priceListBean.size(); c++) {
            if (position == c) {
                priceListBean.get(c).setClick(true);
            } else {
                priceListBean.get(c).setClick(false);
            }
        }
        openVipAdapter.notifyDataSetChanged();
        openVipMoney.setText("合计：￥" + String.format("%.2f", priceListBean.get(position).getVipPrice()));
    }
}
